﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VSSAHT.DataAccess;
using VSSAHT.Transfer;
namespace VSS_AHT
{
    public partial class FormUser : BaseForm
    {
        #region Singleton Members
        private static volatile FormUser instance;
        private static object syncRoot = new Object();

        public static FormUser Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FormUser();
                }

                return instance;
            }
        }
        #endregion

        #region Field Members
        UserDataAccess _dataAccess = new UserDataAccess();

        IList<UserDTO> _userList;
        #endregion

        #region Ctors
        public FormUser()
        {
            InitializeComponent();
            Initializes();
        }
        public void Initializes()
        {
           
            this.btnNew.Click += new EventHandler(delegate { ClickButtonNew(); });
            this.btnEdit.Click += new EventHandler(delegate { ClickButtonEdit(); });
            this.btnRemove.Click += new EventHandler(delegate { ClickButtonDelete(); });

            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
            this.Disposed += new EventHandler(delegate { instance = null; });
            this.ClickButtonSearch();

        }
        #endregion
        #region Methods
        private void ClickButtonSearch()
        {
            _dataAccess.SearchObject = new UserDTO();
            _dataAccess.SearchObject.Username = "";
            

            _userList = _dataAccess.Search();

            this.gridControl1.DataSource = _userList; ;

        }

        

        private void ClickButtonNew()
        {
            int count = _userList.Count;
            // Show the updating staff instance
            FormUpdateUser instance = new FormUpdateUser();
            instance.Collection = _userList;
            instance.ShowDialog();

            if (count < _userList.Count)
            {
                // Refresh data
                this.ClickButtonSearch();
            }

        }

        private void ClickButtonEdit()
        {
            if (this.gridView1.GetFocusedRow() == null)
            {
                Common.ShowAlert("Vui lòng cho dòng cần chỉnh sửa");
                return;
            }

            // Edit  information
            FormUpdateUser instance = new FormUpdateUser();
            instance.User = this.gridView1.GetFocusedRow() as UserDTO;
            DialogResult result = instance.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Refresh data
                this.gridView1.RefreshData();
            }

        }

        private void ClickButtonDelete()
        {
            try
            {
                if (this.gridView1.GetFocusedRow() == null)
                {
                    Common.ShowAlert("Vui lòng chọn dòng cần xóa!");
                    return;
                }

                UserDTO user = this.gridView1.GetFocusedRow() as UserDTO;
                bool? result = Common.ShowYesNoQuestion("Bạn có chắc chắn muốn xóa thông tin này?");
                if (result.HasValue && result.Value)
                {
                    string username = user.Username;
                    bool r = _dataAccess.Delete(user);

                    if (r)
                    {
                        this.gridView1.DeleteSelectedRows();
                        this.gridView1.RefreshData();
                    }
                    else
                    {
                        Common.ShowAlert("Quá trình xóa người dùng không thành công.");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion
    }
}